package vip.xiaonuo.bi.core.datasource.util;

import cn.hutool.core.date.DateUtil;
import lombok.SneakyThrows;

import java.util.Date;

/**
 * 用途: 判断数据类型
 *
 * @Author 顾育司
 * @Data 2023/7/14
 */
public class DateTypePredictUtils {

    //d:维度，q:
   public static String getType(String data){
        if ("BOOL,TIMESTAMP,STRING,DATE".contains(data)) {
            return "x";
        }
        return "y";
    }
    //d:维度，q:
    public static String getETLType(String data){
        if ("BOOL,TIMESTAMP,STRING".contains(data)) {
            return "x";
        }
        return "y";
    }

    public static String  getDataType(String input){
        if (isInteger(input)) {
            return "INT";
        } else if (isDate(input)) {
            return "DATE";
        }else if (isDouble(input)) {
            return "DOUBLE";
        } else if (isTimestamp(input)) {
            return "TIMESTAMP";
        }  else if (isLong(input)) {
            return "LONG";
        } else if (isShort(input)) {
            return "SHORT";
        } else if (isFloat(input)) {
            return "FLOAT";
        }  else if (isBoolean(input)) {
            return "BOOL";
        } else {
            return "STRING";
        }



    }

    /**
     * 迅速判断集合数据数据类型
     * @param input
     * @return
     */
    public static String  getListDataType(String input){
        if (isLong(input)) {
            return "LONG";
        }
        if (isDate(input)) {
            return "DATE";
        }    else if (isFloat(input)) {
            return "FLOAT";
        } else {
            return "STRING";
        }
    }


    private static boolean isInteger(String input) {
        try {
            Integer.parseInt(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private static boolean isDouble(String input) {
        try {
            if(!input.contains(".")){
                return false;
            }
            Double.parseDouble(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private static boolean isString(String input) {
        return true; // 暂定字符串直接返回 true，根据实际情况进行判断
    }

    private static boolean isTimestamp(String input) {
        try {
            //有10位的和13位
            if(!(input.length()==13)){
                return false;
            }
            Long.parseLong(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private static boolean isLong(String input) {
        try {
            Long.parseLong(input);
            return true;
        } catch (NumberFormatException e) {

        }
        return false;
    }

    private static boolean isShort(String input) {
        try {
            Short.parseShort(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private static boolean isFloat(String input) {
        try {
            if(!input.contains(".")){
                return false;
            }
            Float.parseFloat(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    @SneakyThrows
    static boolean isDate(String input){
        try {
            // 尝试使用DateUtil.parse解析日期 自动匹配各种日期模式
            Date date = DateUtil.parse(input==null?"":input);
         }catch (Exception e){
            // 解析失败
            return false;
        }
        return true;
    }
    private static boolean isBoolean(String input) {//『正常测试』
        return input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false");
    }


    public static void main(String[] args) {
        System.out.println(getDataType("2021-12-12"));
    }
}